/**
 * 1.0 cookie---ticket (未加入token)
 *  2019.11.11
 *  qjk
 */
$(function(){
init();
});

function init() {
    //先获取cookie是否有效
    var cookie = getCookie("XXL_JOB_LOGIN_IDENTITY");
    if (cookie) {
        window.location.href = base_url;
    } else {
        var ticket = getUrlParam("ticket");
        var sevice = "http://" + window.location.host + "/xxl-job-admin/toLogin";
        if (ticket) {
            validateSt(ticket, sevice, callback);
        } else {
            var serviceUrl = encodeURIComponent(sevice);
            //console.log(serviceUrl)
            window.location.href = window._CONFIG['casPrefixUrl'] + "/login?service=" + serviceUrl;
        }
    }
}

function callback() {
    console.log(true);
}

function validateSt(ticket,service,callback) {
    var params = {
        ticket: ticket,
        service:service
    };
    $.ajax({
        url: base_url +"/cas/client/validateLogin?ticket="+ticket+"&service="+service,
        type: "get",
        async: true,
        contentType:"application/json",
        dataType: "json",
        //param: JSON.stringify(postData),
        success: function(data){
            if (data.code == "200") {
                layer.msg(I18n.login_success );
                setTimeout(function(){
                    console.log("login success! base_url="+base_url);
                    window.location.href = base_url;
                }, 500);
            } else {
                layer.open({
                    icon: '2',
                    content: (data.msg|| I18n.login_fail)
                });
            }
        },
        error: function(){
            console.log(false)
        },
        complete: function(res){

        }
    });
}

function getUrlParam(paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");

    if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;

        for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");

            if (arr != null && arr[0] == paraName) {
                return arr[1];
            }
        }
        return "";
    }
    else {
        return "";
    }
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name)  == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function login(){
    // 复选框
    $('input').iCheck({
        checkboxClass: 'icheckbox_square-blue',
        radioClass: 'iradio_square-blue',
        increaseArea: '20%' // optional
    });

    // 登陆.规则校验
    var loginFormValid = $("#loginForm").validate({
        errorElement : 'span',
        errorClass : 'help-block',
        focusInvalid : true,
        rules : {
            userName : {
                required : true ,
                rangelength: [4, 18]
            },
            password : {
                required : true ,
                rangelength: [4, 18]
            }
        },
        messages : {
            userName : {
                required  : I18n.login_username_empty,
                minlength : I18n.login_username_lt_4
                //rangelength : "登录账号长度限制为4~18"
            },
            password : {
                required  : I18n.login_password_empty  ,
                minlength : I18n.login_password_lt_4
                //rangelength : "密码长度限制为4~18"
                /*,maxlength:"登录密码不应超过18位"*/
            }
        },
        highlight : function(element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        success : function(label) {
            label.closest('.form-group').removeClass('has-error');
            label.remove();
        },
        errorPlacement : function(error, element) {
            element.parent('div').append(error);
        },
        submitHandler : function(form) {
            $.post(base_url + "/login", $("#loginForm").serialize(), function(data, status) {
                if (data.code == "200") {
                    layer.msg( I18n.login_success );
                    setTimeout(function(){
                        window.location.href = base_url;
                    }, 500);
                } else {
                    layer.open({
                        title: I18n.system_tips,
                        btn: [ I18n.system_ok ],
                        content: (data.msg || I18n.login_fail ),
                        icon: '2'
                    });
                }
            });
        }
    });
}
